package info.jafe.common.http;

import android.text.TextUtils;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jafe chang on 2016/12/31.
 */

public class OkHttpRequestBuilder {
    WeakReference<Object> weakReference;
    String url;
    Map<String, String> header;
    Map<String, String> body;


    OkHttpRequestBuilder(Object refer) {
        weakReference = new WeakReference<>(refer);
    }

    public OkHttpRequestBuilder url(String url) {
        this.url = url;
        return this;
    }

    public OkHttpRequestBuilder query(String key, String value) {
        if (url == null || TextUtils.isEmpty(key)) {
            return this;
        }
        if (!url.contains("?")) {
            url += "?" + combine(key, value);
        } else if (url.endsWith("?")) {
            url += combine(key, value);
        } else {
            url += "&" + combine(key, value);
        }
        return this;
    }

    public OkHttpRequestBuilder query(String key, String... values) {
        if (url == null || TextUtils.isEmpty(key) || values == null) {
            return this;
        }
        for (String value : values) {
            query(key, value);
        }
        return this;
    }

    public OkHttpRequestBuilder header(Map<String, String> header) {
        if (this.header == null) {
            this.header = new HashMap<>();
        }
        if (header != null) {
            this.header.putAll(header);
        }
        return this;
    }

    public OkHttpRequestBuilder header(String key, String value) {
        if (this.header == null) {
            this.header = new HashMap<>();
        }
        if (!TextUtils.isEmpty(key)) {
            this.header.put(key, value);
        }
        return this;
    }

//    public

    private String combine(String key, String value) {
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        return key + "=" + value;
    }


}
